{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d00d9147",
   "metadata": {},
   "source": [
    "# 1. 输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e1d9cb00",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question1():\n",
    "    from datetime import datetime\n",
    "    year, month, day = map(int, input(\"请输入年月日（用空格分隔）: \").split())\n",
    "    date = datetime(year, month, day)\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{date.timetuple().tm_yday}天\"）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1f8834a",
   "metadata": {},
   "source": [
    "# 2. 判断1000中有多少个素数，并输出所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "524d0c46",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question2():\n",
    "    def is_prime(n):\n",
    "        if n <= 1:\n",
    "            return False\n",
    "        for i in range(2, int(n**0.5) + 1):\n",
    "            if n % i == 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "    primes = [i for i in range(2, 1001) if is_prime(i)]\n",
    "    print(f\"1000以内有{len(primes)}个素数，分别是：{primes}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c81689d",
   "metadata": {},
   "source": [
    "# 3. 利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bc59347e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question3():\n",
    "    score = float(input(\"请输入学习成绩: \"))\n",
    "    grade = 'A' if score >= 90 else ('B' if 60 <= score < 90 else 'C')\n",
    "    print(f\"学习成绩等级为：{grade}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "222efc8a",
   "metadata": {},
   "source": [
    "# 4. 输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1165b057",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question4():\n",
    "    s = input(\"请输入一行字符串: \")\n",
    "    letters = sum(1 for c in s if c.isalpha())\n",
    "    spaces = s.count(' ')\n",
    "    digits = sum(1 for c in s if c.isdigit())\n",
    "    others = len(s) - letters - spaces - digits\n",
    "    print(f\"英文字母个数：{letters}，空格个数：{spaces}，数字个数：{digits}，其它字符个数：{others}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1905cd3",
   "metadata": {},
   "source": [
    "# 5. 一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a2602a8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question5():\n",
    "    def get_factors(n):\n",
    "        return [i for i in range(1, n) if n % i == 0]\n",
    "    \n",
    "    perfect_numbers = [i for i in range(2, 1001) if sum(get_factors(i)) == i]\n",
    "    print(f\"1000以内的完数有：{perfect_numbers}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e76f905",
   "metadata": {},
   "source": [
    "# 6. 输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "05febc2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question6():\n",
    "    import math\n",
    "    N = int(input(\"请输入一个任意数N: \"))\n",
    "    factorial_sum = sum(math.factorial(i) for i in range(1, N + 1))\n",
    "    print(f\"1+2!+3!+...+{N}!的值是：{factorial_sum}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8e2ee07",
   "metadata": {},
   "source": [
    "# 7. 输入任意奇数N，按如下要求打印出图案（菱形）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2554457e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question7():\n",
    "    N = int(input(\"请输入一个任意奇数N（控制行数和本行的星数）: \"))\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入一个奇数！\")\n",
    "        return\n",
    "    \n",
    "    mid = N // 2\n",
    "    for i in range(N):\n",
    "        if i <= mid:\n",
    "            print(' ' * (mid - i) + '*' * (2 * i + 1))\n",
    "        else:\n",
    "            print(' ' * (i - mid) + '*' * (2 * (N - i) - 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06cd49fe",
   "metadata": {},
   "source": [
    "\n",
    "# 8. 对10个数进行排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5b110bb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question8():\n",
    "    numbers = []\n",
    "    print(\"请输入10个数：\")\n",
    "    for _ in range(10):\n",
    "        numbers.append(float(input()))\n",
    "    sorted_numbers = sorted(numbers)\n",
    "    print(f\"排序后的数字是：{sorted_numbers}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb06df9e",
   "metadata": {},
   "source": [
    "# 9. 有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fd9138f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question9():\n",
    "    sorted_array = []\n",
    "    print(\"请输入一个已经排好序的数组（用空格分隔，元素数量大于10）：\")\n",
    "    sorted_array = list(map(float, input().split()))\n",
    "    if len(sorted_array) <= 10:\n",
    "        print(\"数组元素数量不大于10，请重新输入！\")\n",
    "        return\n",
    "    \n",
    "    new_number = float(input(\"请输入要插入的数: \"))\n",
    "    \n",
    "    # 找到插入位置\n",
    "    for i in range(len(sorted_array)):\n",
    "        if new_number < sorted_array[i]:\n",
    "            sorted_array.insert(i, new_number)\n",
    "            break\n",
    "    else:\n",
    "        sorted_array.append(new_number)\n",
    "    \n",
    "    print(f\"插入后的数组是：{sorted_array}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2170d1a",
   "metadata": {},
   "source": [
    "# 10. 有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7c03cf8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question10():\n",
    "    n = int(input(\"请输入总人数n: \"))\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 3 - 1) % len(people)\n",
    "        people.pop(index)\n",
    "    print(f\"最后留下的是原来第{people[0]}号的那位\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f64aa63",
   "metadata": {},
   "source": [
    "# 选择题目函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d8790d65",
   "metadata": {},
   "outputs": [],
   "source": [
    "def choose_question():\n",
    "    print(\"请选择题目：\")\n",
    "    for i, func_name in enumerate(globals().keys(), 1):\n",
    "        if func_name.startswith(\"question\"):\n",
    "            print(f\"{i}. {func_name[6:]}\")\n",
    "    \n",
    "    choice = int(input(\"请输入题目编号: \"))\n",
    "    if 1 <= choice <= 10:\n",
    "        globals()[f\"question{choice}\"]()\n",
    "    else:\n",
    "        print(\"无效的题目编号，请重新选择！\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e570e458",
   "metadata": {},
   "source": [
    "# 运行选择题目函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bd00ddc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请选择题目：\n",
      "23. on1\n",
      "25. on2\n",
      "27. on3\n",
      "29. on4\n",
      "31. on5\n",
      "33. on6\n",
      "35. on7\n",
      "37. on8\n",
      "39. on9\n",
      "41. on10\n",
      "请输入题目编号: 1\n",
      "请输入年月日（用空格分隔）: 2022 3 22\n",
      "2022年3月22日是这一年的第81天\n"
     ]
    }
   ],
   "source": [
    "choose_question()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
